package com.cz.run.bumng;

import com.cz.run.bumng.model.AdPictureURL;
import com.cz.run.service.IAdvertisementService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/photo")
public class PhotoUploadController {

    @Value("${spring.images.path}")
    private String images;

    @Autowired
    private IAdvertisementService iAdvertisementService;

    private static  final String url ="http://120.79.249.39:8092";
    /**
     * wangEditor编辑页面跳转
     */
    @RequestMapping("/wangEditor")
    public String wangEditor(){
        return "wangEditor";
    }

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/wangEditorUpload")
    public AdPictureURL upload(@RequestParam("photo") List<MultipartFile> list) {
        Integer errno = 0;
        List<String> urls = new ArrayList<>();
        AdPictureURL returnAd = new AdPictureURL();
        if (list.size() == 0) {
            errno = 1;
        }
        //ImgInfo 将图片存放工程目录的地址存到数据库的操作
        for (MultipartFile file : list) {
            if (file != null) {
                String fileName = UUID.randomUUID().toString();
                File newFile = new File(images+ fileName+".jpg");
                try {
                    FileUtils.copyInputStreamToFile(file.getInputStream(),newFile);
                    String databaseUrl = url+"/img/"+fileName+".jpg";
                    urls.add(databaseUrl);
                } catch (IOException e) {
                    errno = 1;
                    e.printStackTrace();
                }
            }
        }
        returnAd.setData(urls);
        returnAd.setErrno(errno);
        return returnAd;
    }

    @ResponseBody
    @RequestMapping("/uploadImg")
    public Object uploadImg(@RequestParam("file") MultipartFile file) {
        //ImgInfo 将图片存放工程目录的地址存到数据库的操作
            if (file != null) {
                String fileName = UUID.randomUUID().toString();
                File newFile = new File(images+ fileName+".jpg");
                try {
                    FileUtils.copyInputStreamToFile(file.getInputStream(),newFile);
                    String databaseUrl = url+"/img/"+fileName+".jpg";
                    return databaseUrl;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        return null;
    }


    @ResponseBody
    @RequestMapping("/upAdImg")
    public Object upAdImg(@RequestParam("file") MultipartFile file) {
        //ImgInfo 将图片存放工程目录的地址存到数据库的操作

        if (file != null) {
            String fileName = UUID.randomUUID().toString();
            File newFile = new File(images+ fileName+".jpg");
            try {
                FileUtils.copyInputStreamToFile(file.getInputStream(),newFile);
                String databaseUrl = url+"/img/"+fileName+".jpg";
                return databaseUrl;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}